<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head><meta name="generator" content="Hexo 3.8.0">

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--Description-->

    

    
        <meta name="description" content="参考：

https://blog.csdn.net/dxpqxb/article/details/42266873
https://linux.die.net/man/3/mp4setvideoprofilelevel

MP4V2是一个C级别的MP4文件封装工具，网上的资料很多，这里就不讲代码封">
    

    <!--Author-->
    
        <meta name="author" content="Cyrus">
    

    <!--Open Graph Title-->
    
        <meta property="og:title" content="MP4V2的MP4SetTrackESConfiguration和MP4SetVideoProfileLevel">
    

    <!--Open Graph Description-->
    
        <meta property="og:description" content="参考：

https://blog.csdn.net/dxpqxb/article/details/42266873
https://linux.die.net/man/3/mp4setvideoprofilelevel

MP4V2是一个C级别的MP4文件封装工具，网上的资料很多，这里就不讲代码封">
    

    <!--Open Graph Site Name-->
        <meta property="og:site_name" content="Cyrus的技术空间">

    <!--Type page-->
    
        <meta property="og:type" content="article">
    

    <!--Page Cover-->
    
    
        <meta property="og:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

        <meta name="twitter:card" content="summary_large_image">

    

    
        <meta name="twitter:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

    <!-- Title -->
    
    <title>MP4V2的MP4SetTrackESConfiguration和MP4SetVideoProfileLevel - Cyrus的技术空间</title>

    <!-- Bootstrap Core CSS -->
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/blog/css/style.css">

    <!-- Custom Fonts -->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Gallery -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.css" type="text/css" rel="stylesheet">

    <!-- Google Analytics -->
    


    <!-- favicon -->
    

</head>


<body>

    <!-- Menu -->
    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/blog/">Cyrus的技术空间</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                
                    <li>
                        <a href="/blog/">
                            
                                Home
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/archives">
                            
                                Archives
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/tags">
                            
                                Tags
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/categories">
                            
                                Categories
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="https://github.com/klugjo/hexo-theme-clean-blog">
                            
                                <i class="fa fa-github fa-stack-2x"></i>
                            
                        </a>
                    </li>
                
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

    <!-- Main Content -->
    <!-- Page Header -->
<!-- Set your background image for this header in your post front-matter: cover -->

<header class="intro-header" style="background-image: url('http://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg')">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <h1>MP4V2的MP4SetTrackESConfiguration和MP4SetVideoProfileLevel</h1>
                    
                    <span class="meta">
                        <!-- Date and Author -->
                        
                            Posted by Cyrus on
                        
                        
                            2019-10-13
                        
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Tags and categories -->
           
                <div class="col-lg-4 col-lg-offset-2 col-md-5 col-md-offset-1 post-tags">
                    
                        


<a href="/blog/tags/MP4V2/">#MP4V2</a>


                    
                </div>
                <div class="col-lg-4 col-md-5 post-categories">
                    
                        

<a href="/blog/categories/音视频/">音视频</a>

                    
                </div>
            

            <!-- Gallery -->
            

            <!-- Post Main Content -->
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <p>参考：</p>
<ul>
<li><a href="https://blog.csdn.net/dxpqxb/article/details/42266873" target="_blank" rel="noopener">https://blog.csdn.net/dxpqxb/article/details/42266873</a></li>
<li><a href="https://linux.die.net/man/3/mp4setvideoprofilelevel" target="_blank" rel="noopener">https://linux.die.net/man/3/mp4setvideoprofilelevel</a></li>
</ul>
<p>MP4V2是一个C级别的MP4文件封装工具，网上的资料很多，这里就不讲代码封装了，简单地讲一下编译和MP4SetTrackESConfiguration和MP4SetVideoProfileLevel两个方法。</p>
<h3 id="一、编译"><a href="#一、编译" class="headerlink" title="一、编译"></a>一、编译</h3><p>编译的脚本网上也有很多，这里以iOS为例，讲一个最简单的：</p>
<ul>
<li>1、到github <a href="https://github.com/JQJoe/mp4v2-for-iOS" target="_blank" rel="noopener">https://github.com/JQJoe/mp4v2-for-iOS</a>下载仓库；</li>
<li>2、解压仓库文件夹里的mp4v2-2.0.0.tar.bz2；</li>
<li>3、到仓库目录运行 ./build-libmp4v2-for-iOS.sh<br>没错，就是这么简单，其他平台的也可以在github试着找找。</li>
</ul>
<h3 id="二、MP4SetTrackESConfiguration函数"><a href="#二、MP4SetTrackESConfiguration函数" class="headerlink" title="二、MP4SetTrackESConfiguration函数"></a>二、MP4SetTrackESConfiguration函数</h3><p>上面仓库下载的mp4v2的源码里面有一个mp4record的使用demo，使用到的api也不多，照着demo简单地改一改，封装一下就能用了。这里讲一下demo问题的地方：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">if(_naluData[0]==0 &amp;&amp; _naluData[1]==0 &amp;&amp; _naluData[2]==0 &amp;&amp; _naluData[3]==1 &amp;&amp; _naluData[4]==0x67)&#123;</span><br><span class="line">        index = _NALU_SPS_;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    if(index!=_NALU_SPS_ &amp;&amp; recordCtx-&gt;m_vTrackId == MP4_INVALID_TRACK_ID)&#123;</span><br><span class="line">        return index;</span><br><span class="line">    &#125;</span><br><span class="line">    if(_naluData[0]==0 &amp;&amp; _naluData[1]==0 &amp;&amp; _naluData[2]==0 &amp;&amp; _naluData[3]==1 &amp;&amp; _naluData[4]==0x68)&#123;</span><br><span class="line">        index = _NALU_PPS_;</span><br><span class="line">    &#125;</span><br><span class="line">    if(_naluData[0]==0 &amp;&amp; _naluData[1]==0 &amp;&amp; _naluData[2]==0 &amp;&amp; _naluData[3]==1 &amp;&amp; _naluData[4]==0x65)&#123;</span><br><span class="line">        index = _NALU_I_;</span><br><span class="line">    &#125;</span><br><span class="line">    if(_naluData[0]==0 &amp;&amp; _naluData[1]==0 &amp;&amp; _naluData[2]==0 &amp;&amp; _naluData[3]==1 &amp;&amp; _naluData[4]==0x41)&#123;</span><br><span class="line">        index = _NALU_P_;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line"> // 上面这一块nalu判断类型的代码有点问题，这里用的是00 00 00 01开头的nalu格式，但是nalu类型判断并不是0x67/0x68/0x65和0x41,真正的nalu类型判断只取后面5个字节作判断，所以应该是 (_naluData[4] &amp; 0x1F)==0x07/0x08/0x05/0x01。</span><br></pre></td></tr></table></figure></p>
<p>好，下面进入正题，讲一下MP4SetTrackESConfiguration函数，先看demo里面怎么写的：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">MP4SetAudioProfileLevel(recordCtx-&gt;m_mp4FHandle, 0x2);</span><br><span class="line">uint8_t aacConfig[2] = &#123;18,16&#125;;</span><br><span class="line">MP4SetTrackESConfiguration(recordCtx-&gt;m_mp4FHandle,recordCtx-&gt;m_aTrackId,aacConfig,2);</span><br></pre></td></tr></table></figure></p>
<p>这ProfileLevel的0x2代表什么？TrackESConfiguration的{18，16}又是啥？下面让我们来看看具体的定义：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">bool MP4SetAudioProfileLevel(</span><br><span class="line">	MP4FileHandle hFile,</span><br><span class="line">	u_int8_t profileLevel</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">Description</span><br><span class="line">MP4SetAudioProfileLevel sets the minumum profile/level of MPEG-4 audio support necessary to render the contents of the file.</span><br><span class="line"></span><br><span class="line">ISO/IEC 14496-1:2001 MPEG-4 Systems defines the following values:</span><br><span class="line">0x00 Reserved</span><br><span class="line">0x01 Main Profile @ Level 1</span><br><span class="line">0x02 Main Profile @ Level 2</span><br><span class="line">0x03 Main Profile @ Level 3</span><br><span class="line">0x04 Main Profile @ Level 4</span><br><span class="line">0x05 Scalable Profile @ Level 1</span><br><span class="line">0x06 Scalable Profile @ Level 2</span><br><span class="line">0x07 Scalable Profile @ Level 3</span><br><span class="line">0x08 Scalable Profile @ Level 4</span><br><span class="line">0x09 Speech Profile @ Level 1</span><br><span class="line">0x0A Speech Profile @ Level 2</span><br><span class="line">0x0B Synthesis Profile @ Level 1</span><br><span class="line">0x0C Synthesis Profile @ Level 2</span><br><span class="line">0x0D Synthesis Profile @ Level 3</span><br><span class="line">0x0E-0x7F Reserved</span><br><span class="line">0x80-0xFD User private</span><br><span class="line">0xFE No audio profile specified</span><br><span class="line">0xFF No audio required</span><br><span class="line"></span><br><span class="line">// 照描述，说的是设置所支持的最小的profile,这里照demo写0x2,影响不大。</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">bool MP4SetTrackESConfiguration(</span><br><span class="line">MP4FileHandle hFile,</span><br><span class="line">MP4TrackId trackId</span><br><span class="line">const u_int8_t* pConfig,</span><br><span class="line">u_int32_t configSize</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">上面的&#123;18，16&#125;就是pConfig了，2就是configSize。那么&#123;18， 16&#125;代表什么呢？</span><br><span class="line"></span><br><span class="line">首先，config有2个字节组成，共16位，具体含义如下：</span><br><span class="line">5 bits | 4 bits | 4 bits | 3 bits</span><br><span class="line">第一欄    第二欄    第三欄    第四欄</span><br><span class="line"></span><br><span class="line">第一欄：AAC Object Type</span><br><span class="line">第二欄：Sample Rate Index</span><br><span class="line">第三欄：Channel Number</span><br><span class="line">第四欄：Don&apos;t care，設 0</span><br><span class="line"></span><br><span class="line">/* AAC object types */</span><br><span class="line">#define MAIN 1</span><br><span class="line">#define LOW  2</span><br><span class="line">#define SSR  3</span><br><span class="line">#define LTP  4</span><br><span class="line"></span><br><span class="line">/* Returns the sample rate index */</span><br><span class="line">int GetSRIndex(unsigned int sampleRate)</span><br><span class="line">&#123;</span><br><span class="line">   if (92017 &lt;= sampleRate) return 0;</span><br><span class="line">   if (75132 &lt;= sampleRate) return 1;</span><br><span class="line">   if (55426 &lt;= sampleRate) return 2;</span><br><span class="line">   if (46009 &lt;= sampleRate) return 3;</span><br><span class="line">   if (37566 &lt;= sampleRate) return 4;</span><br><span class="line">   if (27713 &lt;= sampleRate) return 5;</span><br><span class="line">   if (23004 &lt;= sampleRate) return 6;</span><br><span class="line">   if (18783 &lt;= sampleRate) return 7;</span><br><span class="line">   if (13856 &lt;= sampleRate) return 8;</span><br><span class="line">   if (11502 &lt;= sampleRate) return 9;</span><br><span class="line">   if (9391 &lt;= sampleRate) return 10;</span><br><span class="line"></span><br><span class="line">   return 11;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">OK，现在再来看&#123;18， 16&#125;是啥：</span><br><span class="line">18 16 --&gt; 0x12 0x10</span><br><span class="line">二进制： 0001 0020 0001 0000</span><br><span class="line">AAC object types： 0001 0 --&gt; 2 就是LOW</span><br><span class="line">Returns the sample rate index: 020 0 --&gt; 4, 就是46009~37566， 其实就是44100</span><br><span class="line">Channel Number： 001 0	--&gt; 2，就是双通道</span><br></pre></td></tr></table></figure>
<h3 id="三、MP4SetVideoProfileLevel函数"><a href="#三、MP4SetVideoProfileLevel函数" class="headerlink" title="三、MP4SetVideoProfileLevel函数"></a>三、MP4SetVideoProfileLevel函数</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">函数原型：</span><br><span class="line">bool MP4SetVideoProfileLevel(</span><br><span class="line">MP4FileHandle hFile,</span><br><span class="line">u_int8_t profileLevel</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">首先，看一下profileLevel都有哪些值：</span><br><span class="line">Description</span><br><span class="line">MP4SetVideoProfileLevel sets the minumum profile/level of MPEG-4 video support necessary to render the contents of the file.</span><br><span class="line"></span><br><span class="line">ISO/IEC 14496-1:2001 MPEG-4 Systems defines the following values:</span><br><span class="line">0x00 Reserved</span><br><span class="line">0x01 Simple Profile @ Level 3</span><br><span class="line">0x02 Simple Profile @ Level 2</span><br><span class="line">0x03 Simple Profile @ Level 1</span><br><span class="line">0x04 Simple Scalable Profile @ Level 2</span><br><span class="line">0x05 Simple Scalable Profile @ Level 1</span><br><span class="line">0x06 Core Profile @ Level 2</span><br><span class="line">0x07 Core Profile @ Level 1</span><br><span class="line">0x08 Main Profile @ Level 4</span><br><span class="line">0x09 Main Profile @ Level 3</span><br><span class="line">0x0A Main Profile @ Level 2</span><br><span class="line">0x0B N-Bit Profile @ Level 2</span><br><span class="line">0x0C Hybrid Profile @ Level 2</span><br><span class="line">0x0D Hybrid Profile @ Level 1</span><br><span class="line">0x0E Basic Animated Texture @ Level 2</span><br><span class="line">0x0F Basic Animated Texture @ Level 1</span><br><span class="line">0x10 Scalable Texture @ Level 3</span><br><span class="line">0x11 Scalable Texture @ Level 2</span><br><span class="line">0x12 Scalable Texture @ Level 1</span><br><span class="line">0x13 Simple Face Animation @ Level 2</span><br><span class="line">0x14 Simple Face Animation @ Level 1</span><br><span class="line">0x15-0x7F Reserved</span><br><span class="line">0x80-0xFD User private</span><br><span class="line">0xFE No audio profile specified</span><br><span class="line">0xFF No audio required</span><br><span class="line"></span><br><span class="line">demo里面写的是</span><br><span class="line">MP4SetVideoProfileLevel(recordCtx-&gt;m_mp4FHandle, 0x7F); //  Simple Profile @ Level 3</span><br><span class="line">参照上面的值，应该是错误的，Simple Profile @ Level 3 应该是0x01。</span><br><span class="line"></span><br><span class="line">关于视频的profileLevel,需要根据实际编码的profile设置，不然会出现解码出来的视频不清晰的问题。</span><br></pre></td></tr></table></figure>

                
            </div>

            <!-- Comments -->
            
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    


                </div>
            
        </div>
    </div>
</article>

    <!-- Footer -->
    <hr>

<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                    

                    

                    
                        <li>
                            <a href="https://github.com/klugjo/hexo-theme-clean-blog" target="_blank">
                                <span class="fa-stack fa-lg">
                                    <i class="fa fa-circle fa-stack-2x"></i>
                                    <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                                </span>
                            </a>
                        </li>
                    

                    

                    

                    
                </ul>
                <p class="copyright text-muted">&copy; 2020 Cyrus<br></p>
                <p class="copyright text-muted">Original Theme <a target="_blank" href="http://startbootstrap.com/template-overviews/clean-blog/">Clean Blog</a> from <a href="http://startbootstrap.com/" target="_blank">Start Bootstrap</a></p>
                <p class="copyright text-muted">Adapted for <a target="_blank" href="https://hexo.io/">Hexo</a> by <a href="http://www.codeblocq.com/" target="_blank">Jonathan Klughertz</a></p>
                <p class="copyright text-muted"><a href="http://www.beian.miit.gov.cn">粤ICP备18110122号-1</a></p>
            </div>
        </div>
    </div>
</footer>


    <!-- After footer scripts -->
    
<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>

<!-- Bootstrap -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<!-- Gallery -->
<script src="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.js" type="text/javascript" charset="utf-8"></script>

<!-- Disqus Comments -->



</body>

</html>